package com.thysdy.flowable.Listener;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.thysdy.flowable.enums.CodeEnum;
import com.thysdy.flowable.exception.MyException;
import com.thysdy.flowable.flowable.BaseProcessService;
import com.thysdy.flowable.service.CommonService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.impl.el.FixedValue;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component(value = "commonListener")
@Slf4j
public class CommonListener extends BaseProcessService implements TaskListener, ExecutionListener {
    private FixedValue url;
    private FixedValue paramString;
    private FixedValue message;
    private FixedValue smsTemplate;
    @Resource
    CommonService commonService;

    /**
     * 任务监听器
     *
     * @param delegateTask
     */
    @SneakyThrows
    @Override
    public void notify(DelegateTask delegateTask) {
        try {
            String serviceUrl = null;
            String myParam = null;
            String mes = null;
            String smsTemplateId = null;
            try {
                serviceUrl = (String) url.getValue(delegateTask);
                url = null;
            } catch (Exception e) {
            }
            try {
                myParam = (String) paramString.getValue(delegateTask);
                paramString = null;
            } catch (Exception e) {
            }
            try {
                mes = (String) message.getValue(delegateTask);
                message = null;
            } catch (Exception e) {
            }
            try {
                smsTemplateId = (String) smsTemplate.getValue(delegateTask);
                smsTemplate = null;
            } catch (Exception e) {
            }
            //获取流程变量
            Map<String, Object> variables = delegateTask.getVariables();
            Map<String, Object> param = null;
            if (StringUtils.isNotBlank(mes)) {
                String assignee = delegateTask.getAssignee();
                Set<IdentityLink> candidates = delegateTask.getCandidates();
                String taskKey = delegateTask.getTaskDefinitionKey();
                Long templateId = null;
                if (null != smsTemplateId) {
                    templateId = Long.valueOf(smsTemplateId);
                }
                commonService.sendMessage(assignee, candidates, taskKey, variables, mes, templateId);
            }
            if (StringUtils.isNotBlank(serviceUrl)) {
                param = new HashMap<>();
                String processInstanceId = delegateTask.getProcessInstanceId();
                String taskDefinitionKey = delegateTask.getTaskDefinitionKey();
                HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
                String businessKey = historicProcessInstance.getBusinessKey();
                param.put("processInstanceId", processInstanceId);
                param.put("businessKey", businessKey);
                param.put("dealUserId", delegateTask.getAssignee());
                param.put("taskDefinitionKey",taskDefinitionKey);
                param.put("variables", variables);
                commonService.callInterface(serviceUrl, myParam, param);
            }
        } catch (MyException e) {
            throw e;
        } catch (Exception e) {
            throw new MyException(CodeEnum.commonException);
        }

    }

    /**
     * 执行监听器
     *
     * @param delegateExecution
     */
    @SneakyThrows
    @Override
    public void notify(DelegateExecution delegateExecution) {
        try {
            String serviceUrl = null;
            String myParam = null;
            Map<String, Object> param = new HashMap<>();
            try {
                if (url != null) {
                    serviceUrl = (String) url.getValue(delegateExecution);
                    url = null;
                }
            } catch (Exception e) {
                throw new MyException(CodeEnum.commonException);
            }
            try {
                myParam = (String) paramString.getValue(delegateExecution);
                paramString = null;
            } catch (Exception e) {
            }
            if (StringUtils.isNotBlank(serviceUrl)) {
                //获取流程变量
                Map<String, Object> variables = delegateExecution.getVariables();
                String processInstanceId = delegateExecution.getProcessInstanceId();
                HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
                String processDefinitionKey = historicProcessInstance.getProcessDefinitionKey();
                String businessKey = historicProcessInstance.getBusinessKey();
                param.put("processInstanceId", processInstanceId);
                param.put("processDefinitionKey", processDefinitionKey);
                param.put("businessKey", businessKey);
                param.put("variables", variables);
                commonService.callInterface(serviceUrl, myParam, param);
            }
        } catch (MyException e) {
            throw e;
        } catch (Exception e) {
            throw new MyException(CodeEnum.commonException);
        }
    }

}
